package top.frsf.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.frsf.reggie.common.R;
import top.frsf.reggie.entity.Category;
import top.frsf.reggie.service.CategoryService;

import java.util.List;

/**
 * @Author FRSF
 * @Date 2023/1/14 23:57
 * @Description 分类控制层
 */

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * @Author: FRSF
     * @Date: 2023/1/15 0:03
     * @Description 新增 菜品/套餐
     * @param category:
     * @return: top.frsf.reggie.common.R<java.lang.String>
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("新增菜品:{}", category.toString());
        categoryService.save(category);
        return R.success("新增分类成功");
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/15 0:17
     * @Description 分类分页查询
     * @param page:     请求当前页码
     * @param pageSize: 每页大小
     * @return: top.frsf.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page < top.frsf.reggie.entity.Category>>
     */
    @GetMapping("/page")
    public R<Page<Category>> page(int page, int pageSize) {
        Page<Category> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getType, Category::getSort);
        categoryService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/15 0:29
     * @Description 删除分类
     * @param ids: 分类ID
     * @return: top.frsf.reggie.common.R<java.lang.String>
     */
    @DeleteMapping
    public R<String> delete(Long ids) {
        log.info("删除分类，ID：{}", ids);
        categoryService.remove(ids);
        return R.success("删除分类成功");
    }


    /**
     * @Author: FRSF
     * @Date: 2023/1/15 1:25
     * @Description 修改套餐信息
     * @param category: 
     * @return: top.frsf.reggie.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        log.info("修改分类信息:{}", category.toString());
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }



    /**
     * @Author: FRSF
     * @Date: 2023/1/16 15:33
     * @Description 套餐下拉框查询
     * @param category:
     * @return: top.frsf.reggie.common.R<java.util.List<top.frsf.reggie.entity.Category>>
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType()!=null,Category::getType,category.getType());
        wrapper.orderByAsc(Category::getType).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }


}
